<?php

namespace App\Http\Controllers;

use App\Http\Model\Hint;
use App\Http\Services\CrossRoomPkService;
use Illuminate\Http\Request;
use App\Lib\Response;
use Carbon\Carbon;
use PDO;

class CrossRoomPkController extends Controller
{
    protected $crossRoomPkService;//房间管理角色
    protected $rule;

    public function __construct()
    {
        $this->crossRoomPkService = new CrossRoomPkService();
        $this->rule = '跨房pk规则：'.PHP_EOL.
        '房主/管理员如何开启PK?'.PHP_EOL.
        '1、每日00:00:00-23:59:59跨房PK限时开启'.PHP_EOL.
        '2、选择随机PK或好友PK'.PHP_EOL.PHP_EOL.
        '玩家如何参与?'.PHP_EOL.
        '1、玩家通过在房间内上麦、发言和送礼物都可以完成每日任务'.PHP_EOL.
        '2、自己房间的PK分,同时增加自己的贡献值'.PHP_EOL.PHP_EOL.
        '参与有什么奖励?'.PHP_EOL.
        '每场PK后，会将玩家的贡献分和房间的PK分进行累计。积分将在榜单中进行展示并根据排名进行展示!'.PHP_EOL.
        PHP_EOL.
        '完成每日任务后,在PK任务中进行领取,奖励自动下发到我的背包'.PHP_EOL.PHP_EOL.
        
        '首攻送礼'.PHP_EOL.
        '一场PK中，率先送出礼物的一方可获得130% 玩家贡献分/房间PK分奖励。';
    }

    //发起PK邀请
    public function sendPkmsg(Request $request){
        //发起者用户id
        $userId = $request->get('user_id',0);
        //发起房间id
        $roomId = $request->get('room_id',0);
        //邀请房间id 
        $toRoomId = $request->get('to_room_id',0);
        //pk类型
        $type = $request->get('pk_type',1);//1定向PK 2=随机
        //判断条件
        if(empty($userId)){
            Response::returnResult(Hint::LOGIN_OUT);
        }
        if(empty($roomId)){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        if(empty($toRoomId) && $type == 1){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //判断发起用户是公会长还是管理员,其他用户不可发起
        $isAdmin = $this->crossRoomPkService->checkRoomAdmin($userId, $roomId);
        if(!in_array($isAdmin,[1,2])){
            Response::returnResult(Hint::AUTH_ERROR);
        }
        //判断房间是否是cbd房间
        $isCBD = $this->crossRoomPkService->checkCbdRoom($roomId);
        if($isCBD != 1){
            Response::returnResult(Hint::ROOM_NOT_CERTIFIED);
        }
        
        //进行邀请房间管理员用户通知
        if($type == 1)
        {
            $sendStatus = $this->crossRoomPkService->sendPkMsg($toRoomId, $roomId, $userId, $type);
        }else{
            $sendStatus = $this->crossRoomPkService->sendRandPkMsg($roomId, $userId, $type);
        }
        
        if($sendStatus['code'] == Hint::SUCCESS){
            Response::returnResult(Hint::SUCCESS);
        }else{
            Response::returnResult(Hint::ERR_NORMAL,[],$sendStatus['msg']);
        }
    }

    //确认/拒绝请求
    public function confirmPkmsg(Request $request){
        $fromRoomId = $request->get('from_roomid',0);//对方房间id
        $myRoomId = $request->get('my_roomid',0);//我方房间id
        $myUserId = $request->get('user_id',0);//接受方userID
        $send_id = $request->get('send_id',0);//对方发起id
        $type = $request->get('status',0);//邀请状态 1=已接受 2=拒绝 3=系统拒绝
        //校验参数
        if(empty($fromRoomId) || empty($myRoomId) || empty($myUserId) || empty($type)){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //权限判断
        $isAdmin = $this->crossRoomPkService->checkRoomAdmin($myUserId, $myRoomId);
        if(!in_array($isAdmin,[1,2])){
            Response::returnResult(Hint::AUTH_ERROR);
        }
        //发起通知给对方
        $sendStatus = $this->crossRoomPkService->confirmPkmsg($fromRoomId, $myRoomId, $myUserId, $type, $send_id);
        if($sendStatus['code'] == Hint::SUCCESS){
            Response::returnResult(Hint::SUCCESS,$sendStatus['data']);
        }else{
            Response::returnResult(Hint::ERR_NORMAL,[],$sendStatus['msg']);
        }
    } 
    
    //取消pk
    public function pkCancel(Request $request){
        $send_id = $request->get('send_id',0);//邀请id
        $userId = $request->get('user_id',0);//操作人
        $roomId = $request->get('room_id',0);//房间id
        if(empty($userId)){
            Response::returnResult(Hint::LOGIN_OUT);
        }
        if(empty($roomId)){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        if(empty($send_id)){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //判断发起用户是公会长还是管理员,其他用户不可发起
        $isAdmin = $this->crossRoomPkService->checkRoomAdmin($userId, $roomId);
        if(!in_array($isAdmin,[1,2])){
            Response::returnResult(Hint::AUTH_ERROR);
        }
        $data = $this->crossRoomPkService->pkCancel($send_id, $userId, $roomId);
        if($data['code'] != Hint::SUCCESS){
            Response::returnResult(Hint::ERR_NORMAL,[],$data['msg']);
        }
        Response::returnResult(Hint::SUCCESS);
    }

    //获取匹配池中房间列表
    public function pkList(Request $request){
        $userId = $request->get('user_id',0);
        $roomId = $request->get('roomId',0);
        $page = $request->get('page',1);//分页
        $limit = $request->get('limit',10);//每页条数
        if(empty($userId)){
            Response::returnResult(Hint::LOGIN_OUT);
        }
        if(empty($roomId)){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $list = $this->crossRoomPkService->getPkList($userId, $roomId, $page, $limit);
        Response::returnResult(Hint::SUCCESS,$list);
    }

    //获取用户每日任务
    public function missionList(Request $request){
        $userId = $request->get('user_id',0);
        if(empty($userId)){
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $minssList = $this->crossRoomPkService->getUserMission($userId);
        Response::returnResult(Hint::SUCCESS,$minssList);
    }

    //用户领取奖励
    public function getPkAward(Request $request){
        $userId = $request->get('user_id',0);//用户id
        if(empty($userId)){
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $award = $request->get('award',0);//领取id
        if(empty($award)){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $status = $this->crossRoomPkService->getPkAward($userId, $award);
        if($status['code'] == Hint::SUCCESS){
            Response::returnResult(Hint::SUCCESS);
        }else{
            Response::returnResult(Hint::ERR_NORMAL,[],$status['msg']);
        }
    }

    //获取榜单
    public function getRank(Request $request){
        $type = $request->get('type',1);//1=房间榜 2=用户榜
        $week = $request->get('week',1);//1=日榜 2=周榜
        $roomId = $request->get('room_id',0);//房间id
        $userId = $request->get('user_id',0);//用户id
        if($type == 1 && empty($roomId)){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        if($type == 2 && empty($userId)){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //获取榜单详情
        $rankList = $this->crossRoomPkService->getRank($type, $week, $roomId, $userId);
        Response::returnResult(Hint::SUCCESS,$rankList);
    }

    //获取实时榜单
    public function getNowRank(Request $request){
        $roomId = $request->get("room_id",0);
        $userId = $request->get('user_id',0);
        if(empty($roomId) || empty($userId)){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //获取榜单详情
        $rankList = $this->crossRoomPkService->getNowRank($roomId, $userId);
        Response::returnResult(Hint::SUCCESS,$rankList);
    }

    //获取房间PK战绩
    public function getRoomRecord(Request $request){
        $roomId = $request->get("room_id",0);
        $page = $request->get('page',1);
        $limit = $request->get('limit',10);
        if(empty($roomId)){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $list = $this->crossRoomPkService->getRoomRecord($roomId, $page, $limit);
        Response::returnResult(Hint::SUCCESS,$list);
    }

    //获取自己房间本周排名
    public function getSelfRoomWeekRank(Request $request){
        $roomId = $request->get("room_id",0);
        $userId = $request->get("user_id",0);
        if(empty($roomId) || empty($userId)){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $list = $this->crossRoomPkService->getSelfRoomRank($roomId, $userId);
        Response::returnResult(Hint::SUCCESS,$list);
    }

    //pk结算
    public function pkEnd(Request $request){
        $recordId = $request->post('send_id',0); //发起记录id
        $type = $request->post('type',0);//是否提前结束PK
        $roomId = $request->post('room_id',0);//操作房间id
        $userId = $request->post('user_id',0);//发起用户id
        if(!empty($type)){
            if(empty($roomId)){
                Response::returnResult(Hint::PARAM_ERROR);
            }
        }
        if(empty($recordId)){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        if(empty($userId)){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //调用结算接口
        $data = $this->crossRoomPkService->endPk($recordId, $type, $roomId, $userId);
        if($data['code'] != Hint::SUCCESS){
            Response::returnResult(Hint::ERR_NORMAL,[],$data['msg']);
        }
        Response::returnResult(Hint::SUCCESS);
    }

    //我的pk
    public function myPk(Request $request){
        $userId = $request->get('user_id',0);
        $roomId = $request->get('room_id',0);
        if(empty($userId)){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //获取我参与过的PK房间
        $pkList = $this->crossRoomPkService->myPk($userId,$roomId);
        Response::returnResult(Hint::SUCCESS,$pkList);
    }

    //进入房间获取PK信息
    public function joinPkroom(Request $request){
        $roomId = $request->get('room_id',0);
        if(empty($roomId)){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //获取PK贡献值
        $data = $this->crossRoomPkService->getJoinPkInfo($roomId);
        if($data['code'] != Hint::SUCCESS){
            Response::returnResult(Hint::ERR_NORMAL,[],$data['msg']);
        }
        Response::returnResult(Hint::SUCCESS,$data['data']);
    }

    //房间规则
    public function pkRule(){
        $data = $this->rule;
        Response::returnResult(Hint::SUCCESS,$data);
    }
}